package com.gitee.wsl.compose.draw.sketch.simulations

import androidx.compose.runtime.Composable
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ExperimentalGraphicsApi
import androidx.compose.ui.graphics.drawscope.translate
import com.gitee.wsl.compose.draw.sketch.k5
import com.gitee.wsl.math.Math.map
import com.gitee.wsl.math.degreesToRadians
import kotlin.math.cos
import kotlin.math.sin
import kotlin.math.sqrt

@Composable
@ExperimentalGraphicsApi
fun showPhyllotaxis() = k5 {
    var n = 0
    val c = 6f
    var start = 0

    onDraw {
        it.translate(dimensFloat.width / 2, dimensFloat.height / 2) {
            for (i in 0 until n) {
                val a = i * 137.5f.degreesToRadians()
                val r = c * sqrt(i.toFloat())
                val x = r * cos(a)
                val y = r * sin(a)
                var hu = sin(start + i * 0.5f)
                hu = map(hu, -1f, 1f, 0f, 360f)
                println(hu)
                this.drawCircle(Color.hsv(hu, 1f, 1f), 4f, Offset(x, y))
            }
        }
        n += 5
        start += 5
    }
}
